<?php
	require_once('init.php');	
	
	//Check Form
	if (isset($_POST['login']) && $_POST['login'] != '') 
	{
		//Query User Name
		if (! $adminDB->executeSQL("select fdLogin, fdName from ".$util->userdbName().".tbUser where fdLogin='".trim($_POST['login'])."'", $connID)) 
		{	
			//Try Insert User
			if (!$adminDB->executeSQL("insert into ".$util->userdbName().".tbUser(fdLogin, fdName, fdCompany, fdPosition, fdPassword) values('".trim($_POST['login'])."', '".trim($_POST['name'])."', '".trim($_POST['company'])."', '".trim($_POST['position'])."', md5('".trim($_POST['password'])."'))", $connID)) 
			{			
				echo "<script>alert('用户添加失败！');</script>";			
			} 
			else 
			{
				//Select New User
				$user = mysql_insert_id();
				//Insert AdminLog
				$adminDB->executeSQL("insert into ".$util->userdbName().".tbAdminLog (fdOperatorID,fdAction,fdTarget,fdUserID,fdTime) values('".$_SESSION['adminid']."', 'INSERT', '".$util->userdbName().".tbUser (fdLogin, fdName, fdCompany, fdPosition, fdPassword) values(".trim($_POST['login']).", ".trim($_POST['name']).", ".trim($_POST['company']).", ".trim($_POST['position']).", md5(".trim($_POST['password'])."))' , '" . $user. "', now())", $connID);
				
				//Check and Insert Address Text
				if (isset($_POST['address']) && $_POST['address'] != ''){
					$sql = "insert into ".$util->userdbName().".tbAddress(fdUserID, fdAddress, fdPostcode, fdDefault) values('".$user."', '".trim($_POST['address'])."', '".trim($_POST['postcode'])."', 0)";
					$adminDB->executeSQL($sql, $connID);
				}
				
				//Check and Insert Telephone Text [Type=0]
				if (isset($_POST['telephone']) && $_POST['telephone'] != ''){
					$sql = "insert into ".$util->userdbName().".tbPhone(fdUserID, fdPhone, fdDefault,fdTypeID) values('".$user."', '".trim($_POST['telephone'])."', 0,0)";
					$adminDB->executeSQL($sql, $connID);
				}
				
				//Check and Insert Mobilephone Text [Type=1]
				if (isset($_POST['mobilephone']) && $_POST['mobilephone'] != ''){
					$sql = "insert into ".$util->userdbName().".tbPhone(fdUserID, fdPhone, fdDefault) values('".$user."', '".trim($_POST['mobilephone'])."', 0,1)";
					$adminDB->executeSQL($sql, $connID);
				}
				
				//Check and Insert Email Text
				if (isset($_POST['email']) && $_POST['email'] != ''){
					$sql = "insert into ".$util->userdbName().".tbEmail(fdUserID, fdEmail, fdDefault) values('".$user."', '".trim($_POST['email'])."', 0)";
					$adminDB->executeSQL($sql, $connID);
				}
				
				echo "<script>alert('用户添加成功！');</script>";
			}
		} 
		else 
		{
			echo "<script>alert('已存在同ID用户！请保证Yiduo ID不重复');</script>";
		}
	}
	
	$smarty->display('admin_adduser.html');	
	$connDB->closeConnID();
	
?>